package com.alan.validate.constraints.Impl;

import com.alan.validate.constraints.Length;
import com.alan.validate.utils.ParamValidate;

public class LengthValidate implements ParamValidate<Length,String> {

	private int min;
	private int max;

	public void initialize(Length parameters) {
		this.min = parameters.min();
		this.max = parameters.max();
		validateParameters();
	}
	
	public boolean isValid(String value) {
		if (value == null) {
			return true;
		}
		int length = value.length();
		return ((length >= this.min) && (length <= this.max));
	}
	
	private void validateParameters() {
		if (this.min < 0) {
			throw new IllegalArgumentException("The min parameter cannot be negative.");
		}
		if (this.max < 0) {
			throw new IllegalArgumentException("The max parameter cannot be negative.");
		}
		if (this.max < this.min)
			throw new IllegalArgumentException("The length cannot be negative.");
	}

}
